package swing.samples.java2sdotcom.progessMonitor.copyexample;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ContentPanel extends JPanel implements ActionListener, Runnable {  
    private JButton srcButton;  
    private JButton targetButton;  
    private JButton copyButton;  
      
    private JTextField srcTextField;  
    private JTextField targetTextField;  
      
    private ProgressBarPanel progressBarPanel;  
      
    private Thread thread;  
      
    private FileInputStream fis;  
    private FileOutputStream fos;  
      
    String fileName = ""; //文件的名字  
      
    public void setProgressBarPanel(ProgressBarPanel progressBarPanel) {  
        this.progressBarPanel = progressBarPanel;  
    }  
  
    public ContentPanel() {  
        srcButton = new JButton("源文件");  
        targetButton = new JButton("目标文件");  
        copyButton = new JButton("拷贝");  
        srcTextField = new JTextField(30);  
        targetTextField = new JTextField(30);  
          
        this.setLayout(null); //设置布局  
      
        this.add(srcTextField);  
        srcTextField.setBounds(55, 30, 140, 30);  
        this.add(srcButton);  
        srcButton.setBounds(195, 30, 80, 30);  
          
        this.add(targetTextField);  
        targetTextField.setBounds(55, 63, 140, 30);  
        this.add(targetButton);  
        targetButton.setBounds(195, 63, 80, 30);  
          
        this.add(copyButton);  
        copyButton.setBounds(195, 96, 80, 30);  
          
        copyButton.addActionListener(this);  
        srcButton.addActionListener(this);  
        targetButton.addActionListener(this);  
    }  
  
    public void actionPerformed(ActionEvent arg0) {  
        // TODO Auto-generated method stub  
        Object o = arg0.getSource();  
          
        if (o == copyButton) {  
              
            String src = srcTextField.getText();  
            String target = targetTextField.getText();  
              
            if ("".equals(src)) {  
                JOptionPane.showMessageDialog(null, "请选择要被拷贝的文件，谢谢！");  
                return;  
            }  
              
            if ("".equals(target)) {  
                JOptionPane.showMessageDialog(null, "请选择目标文件的地址，谢谢！");  
                return;  
            }  
              
            File srcFile = new File(src);  
            System.out.println(target+fileName);  
            File targetFile = new File(target+fileName);  
              
            try {  
                fis = new FileInputStream(srcFile);  
                fos = new FileOutputStream(targetFile);  
            } catch (FileNotFoundException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
              
            thread = new Thread(this);  
            thread.start();  
        }  
          
        if (o == srcButton) { //点击源文件按钮  
            JFileChooser fileChooser = new JFileChooser();  
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); //设置文件选择模式只能打开文件  
            int result = fileChooser.showOpenDialog(this);  
            if (result == 0) {  
                File file = fileChooser.getSelectedFile();  
                String path = file.getAbsolutePath();  
                srcTextField.setText(path);  
                srcTextField.setEditable(false); //设置文本框不可编辑  
                fileName = file.getName();  
            }  
        }  
          
        if (o == targetButton) { //点击目标地址按钮  
            JFileChooser fileChooser = new JFileChooser();  
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);  
            int result = fileChooser.showOpenDialog(this);  
            if (result == 0) {  
                File file = fileChooser.getSelectedFile();  
                String path = file.getAbsolutePath();  
                targetTextField.setText(path);  
                targetTextField.setEditable(false);  
            }  
        }  
    }  
  
    public void run() {  
        // TODO Auto-generated method stub  
        if (progressBarPanel != null) {  
            try {  
                byte[] b = new byte[1024];  
                int size = fis.available();  
                size = size - (size % 1024) + 1024;  
                float per = (float) (100 / (size / 1024.0));  
                int i = 0;  
                CopyEvent e = new CopyEvent();  
                while (true) {  
                    int len = fis.read(b);  
                    if (len == -1) break;  
                    fos.write(b, 0, len);  
                    e.setValue((int)(per * i));  
                    progressBarPanel.addEvent(e);  
                    i++;  
                }  
                fis.close();  
                fos.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
              
        }  
    }  
}  
